let webpack = require('webpack');
let HtmlWebpackPlugin = require('html-webpack-plugin');
let path = require('path');
let UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

process.noDeprecation = true;

module.exports = {
    entry: {
        app: [path.join(__dirname, '../src/App.js')],
        vendor: ['react', 'react-dom', 'dva', 'antd']
    },
    /* devtool: 'source-map', */
    devtool: false,
    output: {
        path: path.join(__dirname, "../dist/"),
        filename: 'js/[name].[chunkhash:8].js',         // 打包后的文件名
        chunkFilename: "[name].[chunkhash:8].chunk.js",
        publicPath: '/'
    },
    externals: {
        // 'ramda': 'R',
        '@antv/data-set': 'DataSet'
    },
    resolve: {
        // require文件的时候不需要写后缀了，可以自动补全
        extensions: ['.js', '.jsx', '.css', '.scss']
    },
    module: {
        rules: [{
            test: /\.(js|jsx|es6)$/,
            exclude: /(node_modules)/,
            use: [{
                loader: 'babel-loader',
                options: {
                    cacheDirectory: true,
                    presets: ['env', 'react'],
                    plugins: [
                        ["import", { libraryName: "antd", style: "css" }],
                        ["transform-runtime"],
                        ["transform-class-properties"],
                        ["transform-async-to-generator"],
                        ["transform-object-rest-spread"]
                    ]
                }
            }]
        },
        { test: /\.css$/, loader: 'style-loader!css-loader' },
        {
            test: /\.less$/,
            use: [{
                loader: 'style-loader' // creates style nodes from JS strings
            }, {
                loader: 'css-loader' // translates CSS into CommonJS
            }, {
                loader: 'less-loader', options: { javascriptEnabled: true } // compiles Less to CSS
            }]
        },
        {
            test: /\.((ttf|eot|woff|svg)(\?t=[0-9]\.[0-9]\.[0-9]))|(ttf|eot|woff|svg)\??.*$/,
            loader: 'url-loader',
            options: {
                limit: 10000,
                name: 'iconfont/[name].[ext]'
            }
        },
        {
            test: /\.(png|jpg|gif)$/,
            loader: 'url-loader',
            options: { 
                limit: 10000,
                name: 'images/[name].[ext]'
            }
        }]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            filename: "js/vendor.[chunkhash:8].js",
            name: ['vendor', 'manifest']
        }),     // 打包公共文件
        new UglifyJsPlugin({
            uglifyOptions: {
                compress: {
                    warnings: false
                },
                output: {
                    comments: false,  // remove all comments
                  },
            }
        }),
        // 压缩JS代码
        new HtmlWebpackPlugin({
            filename: '../dist/index.html',
            hash: true,
            template: 'src/views/index.ejs',
            favicon: './favicon.ico', //注意部署租户公司把这行注释掉，或者换成租户公司的logo
            inject: true,

            //prod
            authcenter: "http://47.99.63.16:8083",
            basedata: "http://47.99.63.16:8082",
            mc: "http://47.99.61.32:8094",

            //test
            // authcenter: 'http://47.99.66.242:8080',
            // mc: 'http://47.99.66.242:8093',
            // basedata: 'http://47.99.66.242:8082'

        }),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('production')
        }),
        new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),
        new BundleAnalyzerPlugin()
        /* "analyz": "NODE_ENV=production npm_config_report=true npm run build" */
    ]
};